package jp.wug.samples;

import jp.wug.Renderer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Select {
    private final static String[] USERS = {"Name1", "Name2", "Name3", "Name4"};
    private Set<Integer> availableUsers = new HashSet<Integer>();
    private int user = -1;

    public Select getForm() {
        return this;
    }

    public boolean hasUserAvailable() {
        return availableUsers.size() > 0;
    }

    public void renderAllUsers(Renderer<Integer, String> renderer) {
        for (int i = 0; i < USERS.length; i++) {
            renderer.render(i, USERS[i]);
        }
    }

    public List<Integer> getAvailableUsers() {
        return new ArrayList<Integer>(availableUsers);
    }

    public void setAvailableUsers(List<Integer> list) {
        HashSet<Integer> set = new HashSet<Integer>();
        set.addAll(list);
        availableUsers = set;
    }

    public int getUser() {
        return user;
    }

    public void setUser(int user) {
        this.user = user;
    }

    public boolean isUserSelected() {
        return user != -1;
    }

    public String getUserName() {
        return USERS[user];
    }

    public void renderUsers(Renderer<Integer, String> renderer) {
        for (int i : availableUsers) {
            renderer.render(i, USERS[i]);
        }
    }

    public void submit() {

    }
}
